package com.tian.mq;

import com.tian.dto.InvitedDto;
import com.tian.message.UserPointMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * @author tianwc  公众号：java后端技术全栈、面试专栏
 * @version 1.0.0
 * @date 2023年05月14日 22:21
 * 博客地址：<a href="http://woaijava.cc/">博客地址</a>
 * <p>
 * 支付订单延迟消息队列
 */
@Component()
@Scope(scopeName = "prototype")
public class UserProducer {
    private final RabbitTemplate rabbitTemplate;

    @Value("${rabbitmq.invited.register.message.exchange.name}")
    private String phoneMessageExchangeName;
    @Value("${rabbitmq.invited.register.message.binding.key}")
    private String phoneMessageBindingKey;

    public UserProducer(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    /**
     * 异步生成邀请记录、邀请收益流水、邀请人可用余额增加
     */
    public void invitedRegister(InvitedDto invitedDto) {
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            /**
             * @param correlationData   相关配置信息
             * @param ack       exchange交换机是否成功收到信息
             * @param cause     失败原因
             */
            @Override
            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
                System.out.println("confirm running ...");
                if (ack) {
                    System.out.println("成功接收");
                } else {
                    //本地表 local_user_invited_register
                    //字段 id、user_id、income、invited_code、create_time、status(0：初始化数据，1：已执行完成)

                    System.out.println("接收失败:" + cause);
                }
            }
        });
        // 发送消息
        rabbitTemplate.convertAndSend(phoneMessageExchangeName, phoneMessageBindingKey, invitedDto);
    }
}


